package com.baj.ldap.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;


public class LdapResult<T> implements Serializable {
    private static final long serialVersionUID = -1L;
    public static final int RESULT_OK;
    private static final int RESULT_ERROR;
    private int code;
    private String message;
    private String detail;
    private long timestamp;
    private T data;
    private JSONObject meta;

    public LdapResult() {
    }

    public static LdapResult ok() {
        return ok(true);
    }

    public static <T> LdapResult<T> ok(T data) {
        LdapResult rj = new LdapResult();
        rj.code = RESULT_OK;
        rj.message = "SUCCESS";
        rj.timestamp = System.currentTimeMillis();
        rj.data = data;
        return rj;
    }

    public static <T> LdapResult<T> ok(T data, JSONObject meta) {
        LdapResult rj = new LdapResult();
        rj.code = RESULT_OK;
        rj.message = "SUCCESS";
        rj.timestamp = System.currentTimeMillis();
        rj.data = data;
        rj.meta = meta;
        return rj;
    }

    public static <T> LdapResult<T> badRequest(int code) {
        LdapResult<T> rj = new LdapResult();
        rj.code = code;
        rj.message = "发生错误!";
        rj.timestamp = System.currentTimeMillis();
        return rj;
    }

    public static <T> LdapResult<T> badRequest(String message) {
        LdapResult<T> rj = new LdapResult();
        rj.code = RESULT_ERROR;
        rj.message = (String) StringUtils.defaultIfEmpty(message, "发生错误!");
        rj.timestamp = System.currentTimeMillis();
        return rj;
    }

    public static <T> LdapResult<T> badRequest(String message, String detail) {
        LdapResult<T> rj = new LdapResult();
        rj.code = RESULT_ERROR;
        rj.message = (String) StringUtils.defaultIfEmpty(message, "发生错误!");
        rj.detail = (String) StringUtils.defaultIfEmpty(detail, "发生错误!");
        rj.timestamp = System.currentTimeMillis();
        return rj;
    }

    public static <T> LdapResult<T> badRequest(int code, String message) {
        LdapResult<T> rj = new LdapResult();
        rj.code = code;
        rj.message = (String) StringUtils.defaultIfEmpty(message, "发生错误!");
        rj.timestamp = System.currentTimeMillis();
        return rj;
    }

    public static <T> LdapResult<T> badRequest(ResponseEnums responseEnums) {
        LdapResult<T> rj = new LdapResult();
        rj.code = responseEnums.code;
        rj.message = responseEnums.desc;
        rj.timestamp = System.currentTimeMillis();
        return rj;
    }

    public static <T> LdapResult<T> badRequest(int code, String message, String detail) {
        LdapResult<T> rj = badRequest(message, detail);
        rj.code = code;
        return rj;
    }

    public static <T> LdapResult<T> badRequest(String message, T data) {
        LdapResult<T> rj = new LdapResult();
        rj.code = RESULT_ERROR;
        rj.message = (String) StringUtils.defaultIfEmpty(message, "发生错误!");
        rj.timestamp = System.currentTimeMillis();
        rj.data = data;
        return rj;
    }

    public boolean isSuccess() {
        return this.code == RESULT_OK;
    }

    public JSONObject toJsonObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", this.code);
        jsonObject.put("message", this.message);
        jsonObject.put("meta", this.meta);
        jsonObject.put("data", this.data);
        jsonObject.put("timestamp", this.timestamp);
        return jsonObject;
    }

    public String toJsonString() {
        return this.toJsonObject().toJSONString();
    }

    public String errorMsg() {
        return this.message + this.detail;
    }

    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    public Date getTimestampDate() {
        return new Date(this.timestamp);
    }

    public String getMessage() {
        return this.message;
    }

    public LdapResult<T> message(String message) {
        this.message = message;
        return this;
    }

    public String getDetail() {
        return this.detail;
    }

    public LdapResult<T> detail(String detail) {
        this.detail = detail;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public LdapResult<T> data(T data) {
        this.data = data;
        return this;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public int getCode() {
        return this.code;
    }

    public LdapResult<T> code(int code) {
        this.code = code;
        return this;
    }

    public JSONObject getMeta() {
        return this.meta;
    }

    public LdapResult<T> meta(JSONObject meta) {
        this.meta = meta;
        return this;
    }

    public String toString() {
        return this.toJsonObject().toString();
    }

    static {
        RESULT_OK = ResponseEnums.SUCCESS.code;
        RESULT_ERROR = ResponseEnums.ERROR.code;
    }


}
